<?php
/**
 * 查询渠道账户资金明细
 * @author chenshubo
 */
namespace App\Service\OrderService\OrderInterfaceService;

use App\Service\BaseService;
use App\Service\Code\Normal;
use App\Model\PssModel\Accountlog;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Arr;

class QueryAccountLog extends BaseService{

    /**
     * 查询渠道账户资金明细
     * @param array $param
     */
    public function index($param = [] ){
        if( empty($param['requestData']['channel_id'])) {
            return Normal::returnCode(Normal::FAIL,"except param requestData.channel_id");
        }

        $channel_id = checkFields($param['requestData']['channel_id']);
        if ( $channel_id != $param['requestData']['channel_id'] ) {
            return Normal::returnCode(Normal::FAIL,"except param requestData.channel_id 不能包含特殊字符");
        }
        // 启用 SQL 数据记录功能
        // Db::enableQueryLog();
        $requestData = $param['requestData'];
        $page  = $requestData['page'] ?? 1;
        $page  = $page >= 1 ? $page -1 : $page;
        $limit = $requestData['limit'] ?? 10;
        $offset = $limit * $page;

        $start_time = $requestData['start_time'] ?? 0 ;
        $end_time   = $requestData['end_time'] ?? 0 ;

        $logModel = Accountlog::join('business', 'accountlog.uid', '=', 'business.id')->where('business.channel_id', $channel_id)->where('business.is_channel', 1);
        if( !empty($start_time) && !empty($end_time) ) {
            $logModel = $logModel->whereBetween('accountlog.cd', [$start_time,$end_time]);
        } elseif( !empty($start_time) ) {
            $logModel = $logModel->where('accountlog.cd', '>=', $start_time);
        } elseif( !empty($end_time) ) {
            $logModel = $logModel->where('accountlog.cd', '<=', $end_time);
        }
        if( !empty($requestData['type']) && isset(Accountlog::$type[$requestData['type']]) ) {
            $logModel = $logModel->where('accountlog.type', '=', $requestData['type']);
        }
        if(isset($requestData['order_sn']) && !empty($requestData['order_sn'])){
            $logModel = $logModel->where('accountlog.order_sn', '=', $requestData['order_sn']);
        }
        if(isset($requestData['supplier_dsn']) && !empty($requestData['supplier_dsn'])){
            $logModel = $logModel->where('accountlog.supplier_dsn', '=', $requestData['supplier_dsn']);
        }

        $fields = ['accountlog.id', 'accountlog.type','accountlog.order_sn', 'accountlog.supplier_dsn','accountlog.symbol','accountlog.amount','accountlog.cd'];
        $logTotal = $logModel->select($fields)->count();
        $result = $logModel->select($fields)->orderBy('accountlog.cd', 'desc')->offset($offset)->limit($limit)->get();
        // 打印最后一条 SQL 相关数据
        // var_dump(Arr::last(Db::getQueryLog()));
        if($result->isEmpty()){
            return Normal::returnCode(Normal::SUC,"获取成功");
        }
        $resultData = $result->each(function ($item, $key){
            $item->amount = number_format($item->amount, 2);
            if( $item->symbol == 1 ) {
                $item->amount = '+' . $item->amount;
            } elseif( $item->symbol == 2 )  {
                $item->amount = '-' . $item->amount;
            } else {
                $item->amount = $item->amount;
            }
            $item->type_text = Accountlog::$type[$item->type];
            $item->remark = Accountlog::$type[$item->type] ?? '金额变化';
            $item->cd_text = $item->cd ? date('Y-m-d H:i:s', $item->cd) : '';
            return $item;
        })->toArray();
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'logList'   => $resultData,
            'logTotal'  => $logTotal
        ]);
    }
}